package cn.lingnan.dao;

import cn.lingnan.entity.goods.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class GoodsDao {
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 查找全部商品
     * @return
     */
    public List<Goods> findAllGoods(){
        List<Goods> goodsList = mongoTemplate.findAll(Goods.class);
        return goodsList;
    }
    /**
     * 创建对象
     */
//    public void saveTest(MongoTest test) {
//        mongoTemplate.save(test);
//    }

    /**
     * 根据goods_id查询对象
     * @return
     */
    public Goods findGoodsById(int goods_id) {
        Query query=new Query(Criteria.where("goods_id").is(goods_id));
        Goods goods =  mongoTemplate.findOne(query , Goods.class);
        return goods;
    }

    /**
     * 更新对象
     */
//    public void updateTest(MongoTest test) {
//        Query query=new Query(Criteria.where("id").is(test.getId()));
//        Update update= new Update().set("age", test.getAge()).set("name", test.getName());
//        //更新查询返回结果集的第一条
//        mongoTemplate.updateFirst(query,update,MongoTest.class);
//        //更新查询返回结果集的所有
//        // mongoTemplate.updateMulti(query,update,TestEntity.class);
//    }

    /**
     * 删除对象
     * @param id
     */
//    public void deleteTestById(Integer id) {
//        Query query=new Query(Criteria.where("id").is(id));
//        mongoTemplate.remove(query,MongoTest.class);
//    }
}
